//
//  PostViewController.m
//  ZhuanZuBao
//
//  Created by 戴伟 on 2020/9/5.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "PostViewController.h"
#import "PostImagesViewController.h"
#import "TZImagePickerController.h"
#import "FJFKeyboardHelper.h"
#import "PostTagViewController.h"
#import "ChooseRoomTypeViewController.h"
#import "ChooseFloorViewController.h"
#import "ChooseLocationViewController.h"
#import "ChooseDateViewController.h"
#import <AMapLocationKit/AMapLocationKit.h>

@interface PostViewController ()<TZImagePickerControllerDelegate, UITextFieldDelegate>



@property (strong, nonatomic) IBOutlet UIView *detailView;
@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;
@property (nonatomic, strong) UIImage *mainImage; //主图
@property (nonatomic, strong) NSMutableArray *imageArr; //附图数组

@property (weak, nonatomic) IBOutlet UIButton *uploadBtn1;
@property (weak, nonatomic) IBOutlet UIButton *uploadBtn2;
@property (weak, nonatomic) IBOutlet UIButton *roomTypeBtn;
@property (weak, nonatomic) IBOutlet UIButton *floorBtn;

@property (weak, nonatomic) IBOutlet UITextField *titleTextField;
@property (weak, nonatomic) IBOutlet UITextField *commutinityTextField;
@property (weak, nonatomic) IBOutlet UITextField *metroTextField;
@property (weak, nonatomic) IBOutlet UITextField *moneyTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UITextField *areaTextField;

@property (nonatomic, copy) NSString *lat;
@property (nonatomic, copy) NSString *lon;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *district;

@end

@implementation PostViewController

-(NSMutableArray *)imageArr{
    if (!_imageArr) {
        _imageArr = [NSMutableArray array];
    }
    return _imageArr;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人转租";
    
    UIScrollView *scrollView = [UIScrollView new];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    
    UIView *containerView = [UIView new];
    [scrollView addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];
    
    [containerView addSubview:_detailView];
    [_detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_offset(0);
        make.height.mas_equalTo(825);
    }];
    
    [containerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.detailView.mas_bottom).mas_offset(40);
    }];
    
    
    _uploadBtn2.hidden = YES;
    
    [FJFKeyboardHelper handleKeyboardWithContainerView:self.view];
    
}
- (IBAction)edit:(id)sender {
    //进入图片编辑界面
    PostImagesViewController *vc = [[PostImagesViewController alloc] initWithMainImage:_mainImage imageArr:[_imageArr copy]];
    vc.selectImageBlock = ^(UIImage * _Nonnull mainImage, NSArray * _Nonnull imageArr) {
        self.mainImage = mainImage;
        self.headerImageView.image = mainImage;
        [self.imageArr removeAllObjects];
        [self.imageArr addObjectsFromArray:imageArr];
        self.uploadBtn2.hidden = NO;
        self.uploadBtn1.hidden = YES;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 选择图片
- (IBAction)upload:(id)sender {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    imagePickerVc.allowPickingVideo = NO;
    [self presentViewController:imagePickerVc animated:YES completion:nil];
    
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
    PostImagesViewController *vc = [[PostImagesViewController alloc] initWithMainImage:photos[0] imageArr:@[]];
    vc.selectImageBlock = ^(UIImage * _Nonnull mainImage, NSArray * _Nonnull imageArr) {
        self.mainImage = mainImage;
        self.headerImageView.image = mainImage;
        [self.imageArr removeAllObjects];
        [self.imageArr addObjectsFromArray:imageArr];
        self.uploadBtn2.hidden = NO;
        self.uploadBtn1.hidden = YES;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 下一步，填写附加信息
- (IBAction)next:(id)sender {
    if (!_mainImage) {
        [MBProgressHUD showWarning:@"请选择主图" toView:nil];
        return;
    }
    if(_titleTextField.text.length == 0){
        [MBProgressHUD showWarning:@"请输入标题" toView:nil];
        return;
    }
    
    if(_commutinityTextField.text.length == 0){
        [MBProgressHUD showWarning:@"请选择小区" toView:nil];
        return;
    }
    if([_roomTypeBtn.titleLabel.text isEqualToString:@"请选择"]){
        [MBProgressHUD showWarning:@"请选择户型" toView:nil];
        return;
    }
    if([_floorBtn.titleLabel.text isEqualToString:@"请选择"]){
        [MBProgressHUD showWarning:@"请选择楼层" toView:nil];
        return;
    }
    
    if(_metroTextField.text.length == 0){
        [MBProgressHUD showWarning:@"请输入地铁" toView:nil];
        return;
    }
    if(_moneyTextField.text.length == 0){
        [MBProgressHUD showWarning:@"请输入每月租金" toView:nil];
        return;
    }
    if(_areaTextField.text.length == 0){
        [MBProgressHUD showWarning:@"请输入面积" toView:nil];
        return;
    }
    if(_phoneTextField.text.length == 0){
        [MBProgressHUD showWarning:@"请输入联系号码" toView:nil];
        return;
    }
//    if(_dateTextField.text.length == 0){
//        [MBProgressHUD showWarning:@"请输入到期时间" toView:nil];
//        return;
//    }
//    
    
    PostTagViewController *vc = [PostTagViewController new];
    vc.postTitle = self.titleTextField.text;
    
    vc.phone = self.phoneTextField.text;
    vc.money = self.moneyTextField.text;
    vc.metro = self.metroTextField.text;
    vc.communityName = self.commutinityTextField.text;
    vc.postType = _postType;
    vc.floor = _floorBtn.titleLabel.text;
    vc.roomType = _roomTypeBtn.titleLabel.text;
    vc.area = _areaTextField.text;
    vc.mainImage = _mainImage;
    vc.imageArr = _imageArr;
    vc.lon = _lon;
    vc.lat = _lat;
    vc.city = _city;
    vc.district = _district;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 选择户型
- (IBAction)chooseRoomType:(id)sender {
    ChooseRoomTypeViewController *vc = [ChooseRoomTypeViewController new];
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    vc.selectRoomTypeBlock = ^(NSString * _Nonnull roomType) {
        [self.roomTypeBtn setTitle:roomType forState:UIControlStateNormal];
        [self.roomTypeBtn setTitleColor:BLACKTEXTCOLOR forState:UIControlStateNormal];
    };
    [self presentViewController:vc animated:NO completion:nil];
}
#pragma mark - 选择楼层
- (IBAction)chooseFloor:(id)sender {
    ChooseFloorViewController *vc = [ChooseFloorViewController new];
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    vc.selectFloorBlock = ^(NSString * _Nonnull floor) {
        [self.floorBtn setTitle:floor forState:UIControlStateNormal];
        [self.floorBtn setTitleColor:BLACKTEXTCOLOR forState:UIControlStateNormal];
    };
    [self presentViewController:vc animated:NO completion:nil];

}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == _commutinityTextField) {
        if ([User sharedUser].currentCity && [User sharedUser].currentCity.length >0) {
            ChooseLocationViewController *vc = [ChooseLocationViewController new];
            vc.chooseCompleteBlock = ^(AMapPOI *location) {
                self.commutinityTextField.text = location.name;
                self.lat = [NSString stringWithFormat:@"%f", location.location.latitude];
                self.lon = [NSString stringWithFormat:@"%f", location.location.longitude];
                self.city = location.city;
                self.district = location.district;
            };
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [MBProgressHUD showWarning:@"请在首页选择城市" toView:nil];
        }
        
        return NO;
    }
    return YES;
}

@end
